package de.maedn.main.server;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		String thisURL = req.getRequestURI();
		resp.setContentType("text/html");
		if (userService.isUserLoggedIn()) {
			Principal principal = req.getUserPrincipal();
			resp.getWriter().println("<p>Hello, " + principal.getName() + "! You can <a href=\"" + userService.createLogoutURL(thisURL)+
			"\">sign out</a>.</p>");
		} else {
			resp.getWriter().println("<p>Please <a href=\"" + userService.createLoginURL(thisURL)+ "\">sign in</a>.</p>");
		}
	}
	
}
